package com.link.play.fit.ai.ui.main.saveFood.details

import android.Manifest
import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.bean.ByDesFixBean
import com.link.play.fit.ai.bean.CollectFoodInfoData
import com.link.play.fit.ai.bean.FixByIssueBean
import com.link.play.fit.ai.bean.FoodInfoBean
import com.link.play.fit.ai.bean.RecentlyEatenData
import com.link.play.fit.ai.databinding.FixResultDetailLayoutBinding
import com.link.play.fit.ai.databinding.FixResultLayoutBinding
import com.link.play.fit.ai.ext.checkPermissions
import com.link.play.fit.ai.ext.d
import com.link.play.fit.ai.store.CloudStore
import com.link.play.fit.ai.store.CloudStore.DOC_SAVE_FOOD_COLLECT_DATA
import com.link.play.fit.ai.utils.BitmapUtils.getScreenBitmap
import com.link.play.fit.ai.utils.BitmapUtils.saveBitmapToFile
import com.link.play.fit.ai.utils.ShareFileUtils.getImageMediaId
import com.link.play.fit.ai.utils.ShareFileUtils.shareFile
import com.link.play.fit.ai.utils.ShareFileUtils.shareMedia
import kotlinx.coroutines.launch
import java.io.File
import kotlin.math.abs

class FixResultDetailAc :
    BaseActivity<FixResultDetailLayoutBinding>(FixResultDetailLayoutBinding::inflate) {
    private var collectFoodInfoData: CollectFoodInfoData? = null
    private var mByDesFixBean: FixByIssueBean? = null
    private var oldCal = 0.0
    private var oldCarbs = 0.0
    private var oldProtein = 0.0
    private var oldFats = 0.0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initListener()
        initData()
    }

    @SuppressLint("NewApi", "SetTextI18n")
    private fun initData() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            collectFoodInfoData = intent.getSerializableExtra("foodBean", CollectFoodInfoData::class.java)
            mByDesFixBean = intent.getSerializableExtra("fixBean", FixByIssueBean::class.java)
        } else {
            collectFoodInfoData = intent.getSerializableExtra("foodBean") as CollectFoodInfoData?
            mByDesFixBean = intent.getSerializableExtra("fixBean")as FixByIssueBean?
        }
        "mByDesFixBean $mByDesFixBean".d()
        collectFoodInfoData?.let {
            binding.nameText.text = it.name
            binding.caloriesValueText.text = "${it.calories}"
            binding.carbsValueText.text = "${it.carbs}g"
            binding.proteinValueText.text = "${it.protein}g"
            binding.fatsValueText.text = "${it.fats}g"
            oldCal = it.calories
            oldCarbs = it.carbs
            oldProtein = it.protein
            oldFats = it.fats
        }
        mByDesFixBean?.let {

            collectFoodInfoData?.apply {
                this.calories = it.calories?:0.0
                this.carbs = it.carbs?:0.0
                this.protein = it.protein?:0.0
                this.fats = it.fats?:0.0
            }
            with(binding){
                val currentCal = it.calories?:0.0
                val value = currentCal - oldCal
                if(value>0){
                    editCaloriesValueText.isVisible = true
                    editCaloriesValueText.text = "+${value}"
                }else if(value<0){
                    editCaloriesValueText.isVisible = true
                    editCaloriesValueText.text = "-${abs(value)}"
                }else{
                    editCaloriesValueText.isVisible = false
                }

                val currentCar = it.carbs?:0.0
                val valueCarbs = currentCar - oldCarbs
                if(valueCarbs>0){
                    editcarbsValueText.isVisible = true
                    editcarbsValueText.text = "+${valueCarbs}"
                }else if(valueCarbs<0){
                    editcarbsValueText.isVisible = true
                    editcarbsValueText.text = "-${abs(valueCarbs)}"
                }else{
                    editcarbsValueText.isVisible = false
                }

                val currentPro = it.protein?:0.0
                val valuePro = currentPro - oldProtein
                if(valuePro>0){
                    editproteinValueText.isVisible = true
                    editproteinValueText.text = "+${valuePro}"
                }else if(valuePro<0){
                    editproteinValueText.isVisible = true
                    editproteinValueText.text = "-${abs(valuePro)}"
                }else{
                    editproteinValueText.isVisible = false
                }

                val currentFats = it.fats?:0.0
                val valueFats = currentFats - oldFats
                if(valueFats>0){
                    editfatsValueText.isVisible = true
                    editfatsValueText.text = "+${valueFats}"
                }else if(valueFats<0){
                    editfatsValueText.isVisible = true
                    editfatsValueText.text = "-${abs(valueFats)}"
                }else{
                    editfatsValueText.isVisible = false
                }

            }

        }
        saveCollect()
    }

    private fun initListener() {
        with(binding) {
            backImage.setOnClickListener {
                finish()
            }
            shareText.setOnClickListener {
                var imgFile:File?
                val bitmap = getScreenBitmap(binding.contentLayout)
                lifecycleScope.launch {
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
                        val isPermissions = this@FixResultDetailAc.checkPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        if(isPermissions){
                            imgFile = saveBitmapToFile(bitmap,this@FixResultDetailAc)
                            imgFile?.let {
                                shareFile(this@FixResultDetailAc, it)
                            }
                        }
                    }else{
                        imgFile = saveBitmapToFile(bitmap,this@FixResultDetailAc)
                        val mediaId = getImageMediaId(this@FixResultDetailAc)
                        imgFile?.let {
                            mediaId?.let { k->
                                shareMedia(this@FixResultDetailAc, k)
                            }
                        }
                    }
                }
            }
        }
    }

    private fun saveCollect(){
        collectFoodInfoData?.let {
            CloudStore.addToList(
                DOC_SAVE_FOOD_COLLECT_DATA,
                date = DOC_SAVE_FOOD_COLLECT_DATA,
                key = it.timeMillisecond,
                data = it
            )
        }
    }


}